package com.fog.student.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fog.student.common.Base;
import com.fog.student.dto.ClassDto;
import com.fog.student.dto.CourseDto;
import com.fog.student.entity.Course;
import com.fog.student.entity.Student;
import com.fog.student.entity.Teacher;
import com.fog.student.service.ClassService;
import com.fog.student.service.CourseService;
import com.fog.student.service.StudentService;
import com.fog.student.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping("/teacher")
public class TeacherController {
    @Resource
    private TeacherService teacherService;

    @Resource
    private CourseService courseService;

    @Resource
    private ClassService classService;
    @GetMapping("/page")
    public Base page(int page , int pageSize , String name){
        //log.info(UserContext.getUserId().toString());
        // 创建分页
        Page<Teacher> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.orderByDesc("username");
        if(name !=null){
            teacherQueryWrapper.like("name", name);
        }
        teacherService.page(pageInfo, teacherQueryWrapper);
        return Base.success(pageInfo);
    }
    @GetMapping("/{id}")
    public Base getEmployeeById(@PathVariable Long id){
        Teacher teacher = teacherService.getById(id);
        return Base.success(teacher);
    }
    @PutMapping
    public Base changeStatusTeacher(@RequestBody Teacher teacher){
        teacherService.saveOrUpdate(teacher);
        return Base.success("更新成功");
    }
    @PostMapping
    public Base addTeacher(HttpServletRequest request, @RequestBody Teacher teacher){
        // 1. 查询教师是否储存
        Teacher one = teacherService.getOne(new QueryWrapper<Teacher>().eq("username", teacher.getUsername()));
        if (one != null){
            return  Base.error( teacher.getUsername() + "用户已经存在");
        }
        // 2. 填写创建用户信息，填写初始化密码
        teacher.setPassword("123456");
        teacher.setClassId(teacher.getClassId());
        // 3.存入到数据库
        teacherService.save(teacher);
        return Base.success("添加用户成功");
    }
    @GetMapping("/list")
    public Base list(){
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();

        List<Teacher> list = teacherService.list(teacherQueryWrapper);

        log.info("教师信息:"+list.toString());

        return Base.success(list);
    }
    @GetMapping("/course")
    public Base getTeacherCourse(int page , int pageSize , String name){
        Page<Course> pageInfo = courseService.selectTeacherCourse(page,pageSize,name);
        return Base.success(pageInfo);
    }

    @GetMapping("/class")
    public Base getTeacherClass(int page , int pageSize , String name){

        Page<ClassDto> pageInfo = classService.selectTeacherClass(page,pageSize,name);

        return Base.success(pageInfo);
    }

    @PostMapping("/status/{status}")
    public Base changeStatus(@PathVariable int status,String ids){
        String[] idList = ids.split(",");
        for (String id : idList) {
            Course course = new Course();
            course.setId(Integer.parseInt(id));
            course.setStatus(status);
            courseService.updateById(course);
        }

        return Base.success("更新状态成功");
    }
}
